package com.persistent.rest;

import java.util.List;
import java.util.StringTokenizer;

import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.persistent.entity.Person;
import com.persistent.service.PersonService;

// The Java class will be hosted at the URI path "/persons"

@Component
@Path("/persons_")
@Scope("request")
public class PersonPathResourceWs implements PersonResource {
	@Autowired
	PersonService personService;
	private Logger log = Logger.getLogger(this.getClass());
	// The Java method will process HTTP GET requests

	@GET
	@Produces("application/json")
	public String getPersons() {
		StringBuffer jsonBuffer = new StringBuffer("{ \"Users\": [");
		List<Person> persons = personService.getAll();
		boolean first = true;
		for (Person person : persons) {
			if (first)
				first = false;
			else
				jsonBuffer.append(",");
			jsonBuffer.append(getJsonPersonObject(person));
		}
		jsonBuffer.append("]}");
		log.info("Sending: \n\n" + jsonBuffer.toString() + "\n\n");
		return jsonBuffer.toString();
	}

	@GET
	@Path("{id}")
	@Produces("application/json")
	public String getPerson(@PathParam("id") String id) {
		log.info("Hit getPerson");
		Person person = personService.getById(Integer.parseInt(id));
		String result = (String) getJsonPersonObject(person);
		log.info("Sending: \n\n" + result + "\n\n");
		return result;
	}

	@PUT
	@Path("/id/{id}/name/{name}/age/{age}")
	@Produces("text/plain")
	public String updatePerson(@PathParam("id") String id,
			@PathParam("name") String name, @PathParam("age") String age) {
		log.info("name: " + name);
		log.info("age : " + age);
		log.info("id : " + id);

		if (StringUtils.hasText(id)) {
			log.info("It has an ID");
			int idd = Integer.parseInt(id);
			Person person = personService.getById(idd);
			if (person != null) {
				log.info("We found the person with that id");
				person.setAge(Integer.parseInt(age));
				person.setName(name);
				personService.update(person);
			}
		} else {
			Person person = new Person();
			person.setName(name);
			person.setAge(Integer.parseInt(age));
			personService.save(person);
		}
		return "nothing";
	}

	@POST
	@Path("/name/{name}/age/{age}")
	@Produces("text/plain")
	public String addPerson(@PathParam("name") String name,
			@PathParam("age") String age) {
		log.info("name: " + name);
		log.info("age : " + age);

		Person person = new Person();
		person.setName(name);
		person.setAge(Integer.parseInt(age));
		personService.save(person);
		return "nothing";
	}

	@GET
	@Path("/clear/{clear}")
	@Produces("text/plain")
	public String refresh(@PathParam("clear") String clear) {
		return "nothing";
	}
	
	@DELETE
	@Path("{ids}")
	@Produces("text/plain")
	public String deleteCustomer(@PathParam("ids") String ids) {
		log.info("The ids are: " + ids);
		if (ids == null)
			return "failed";
		StringTokenizer stringTokenizer = new StringTokenizer(ids, ",");
		while (stringTokenizer.hasMoreElements()) {
			String id = (String) stringTokenizer.nextElement();
			log.info("Delete Id: " + id);
			Person person = personService.getById(Integer.parseInt(id));
			if (person == null) {
				log.info("Null was returned for ID: " + id);
			} else {
				personService.delete(person);
			}
		}
		return "success";
	}

	private Object getJsonPersonObject(Person person) {
		StringBuffer buffer = new StringBuffer("{\"id\": " + person.getId());
		buffer.append(",\"name\": \"").append(person.getName());
		buffer.append("\",\"age\": ").append(person.getAge());
		buffer.append("}");
		return buffer.toString();
	}


	public String deletePerson(String ids) {
		// TODO Auto-generated method stub
		return null;
	}
}
